package com.haois.mp.database.module.offaccounts.rest;

import com.haois.mp.database.exception.ErrorException;
import com.haois.mp.database.exception.OutException;
import com.haois.mp.database.module.offaccounts.controller.GroupController;
import me.chanjar.weixin.common.exception.WxErrorException;
import com.haois.mp.database.module.basic.controller.BaseController;
import com.haois.mp.database.module.remote.service.IGroupService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by RuiHaoZ on 2017/12/5.
 */
@RestController
@RequestMapping("/wxmp/rest/group")
public class GroupRestController extends BaseController{

    private static final Logger logger = LoggerFactory
            .getLogger(GroupController.class);

    @Autowired
    private IGroupService iGroupService;

    @RequestMapping(value = "/getDepTree", method = RequestMethod.GET)
    public Map getDepTree(String oid, HttpServletRequest request) throws
            RuntimeException, WxErrorException, ErrorException {
        String access_token = (String) request.getAttribute("access_token");
        Map map = iGroupService.getDepTree(oid, access_token);
        return map;
    }

    @RequestMapping(value = "/getGroupList", method = RequestMethod.GET)
    public Map getGroupList(HttpServletRequest request,String groupType) throws
            RuntimeException, WxErrorException, ErrorException {
        String access_token = (String) request.getAttribute("access_token");
        Map map = iGroupService.getGroupList(groupType, access_token,getPagerParams(request));
        return map;
    }

    @RequestMapping(value = "/getGroup", method = RequestMethod.GET)
    public Map getGroup(String oid, HttpServletRequest request) throws
            RuntimeException, WxErrorException, ErrorException {
        String access_token = (String) request.getAttribute("access_token");
        Map map = iGroupService.getGroup(oid, access_token);
        return map;
    }

    @RequestMapping(value = "/exitGroup", method = RequestMethod.POST)
    public Map exit(String oid, HttpServletRequest request) throws
            RuntimeException, WxErrorException, ErrorException {
        String access_token = (String) request.getAttribute("access_token");
        Map map = iGroupService.exitGroup(oid, access_token);
        return map;
    }

    @RequestMapping(value = "/authorize", method = RequestMethod.POST)
    public Map updateMemOrg(String oid, HttpServletRequest request, String type) throws
            RuntimeException, WxErrorException, ErrorException {
        String access_token = (String) request.getAttribute("access_token");
        Map map = new HashMap();
        if ("memAuth".equals(type)) {
            map = iGroupService.updateAuthorizeMemState(oid, access_token);
        } else if ("orgAuth".equals(type)) {
            map = iGroupService.updateAuthorizeOrgState(oid, access_token);
        } else {
            map.put("code","10");
            map.put("msg","参数有误");
        }
        return map;
    }

    @RequestMapping(value = "/join", method = RequestMethod.POST)
    public Map follow(String oid, HttpServletRequest request, String mark) throws
            RuntimeException, WxErrorException, ErrorException {
        String access_token = (String) request.getAttribute("access_token");
        Map map = null;
        if (StringUtils.isNotBlank(oid)) {
            map = iGroupService.joinGroup(oid, access_token);
        } else {
            map = iGroupService.joinGroupByMark(mark, access_token);
        }

        return map;
    }

    @RequestMapping(value = "/relation", method = RequestMethod.GET)
    public Map relation(String oid, HttpServletRequest request, String mark, String memberNum) throws
            RuntimeException, WxErrorException, ErrorException {
        String access_token = (String) request.getAttribute("access_token");
        Map map = null;
        memberNum = StringUtils.isBlank(memberNum) ? "" : memberNum;
        if (StringUtils.isNotBlank(oid)) {
            map = iGroupService.getGroupRelation(oid, memberNum, access_token);
        } else {
            map = iGroupService.getGroupRelationByMark(mark, memberNum, access_token);
        }
        return map;
    }

    @RequestMapping(value = "/mobile")
    public Map mobile(String oid, HttpServletRequest request) throws WxErrorException, OutException {
        String access_token = (String) request.getAttribute("access_token");
        Map result = iGroupService.getMemberByOid(oid, access_token);
        return result;
    }

    @RequestMapping(value = "/sendApply", method = RequestMethod.POST)
    public Map sendApply(String oid, HttpServletRequest request, String content, String excuse) throws
            RuntimeException, WxErrorException, ErrorException {
        String access_token = (String) request.getAttribute("access_token");
        Map result = iGroupService.sendApply(oid, content, excuse, access_token);

        return result;
    }

}
